package com.work.yshop.adapter;

import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.work.yshop.R;

import java.util.ArrayList;
import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {

    private List<Message> messages = new ArrayList<>();
    private OnItemClickListener listener;

    public void setData(List<Message> messages) {
        this.messages = messages;
        notifyDataSetChanged();
    }

    public Message getItem(int position) {
        return messages.get(position);
    }

    @NonNull
    @Override
    public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, parent, false);
        return new MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
        Message message = messages.get(position);
        holder.titleView.setText(message.getTitle());
        holder.contentView.setText(message.getContent());
        holder.timeView.setText(message.getTime());
        
        // 在实际应用中，这里会使用Glide加载图片
        // Glide.with(holder.itemView.getContext()).load(message.getImageUrl()).into(holder.avatarView);
        
        holder.unreadIndicator.setVisibility(message.isUnread() ? View.VISIBLE : View.GONE);
        
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    static class MessageViewHolder extends RecyclerView.ViewHolder {
        TextView titleView;
        TextView contentView;
        TextView timeView;
        ImageView avatarView;
        View unreadIndicator;

        public MessageViewHolder(@NonNull View itemView) {
            super(itemView);
            titleView = itemView.findViewById(R.id.message_title);
            contentView = itemView.findViewById(R.id.message_content);
            timeView = itemView.findViewById(R.id.message_time);
            avatarView = itemView.findViewById(R.id.message_avatar);
            unreadIndicator = itemView.findViewById(R.id.unread_indicator);
        }
    }
} 